﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
//using System.Collections;
using System.Xml;
using System.Web.Services;

public partial class Comod_OutManage : ATPages
{
    Function f = new Function();
    ZSql zs = new ZSql();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            yjTm1.Attributes.Add("onclick", "SelectDate('yjTm1')");
            yjTm2.Attributes.Add("onclick", "SelectDate('yjTm2')");
            HFroid.Value = Request.Cookies["user"].Values["roleid"];
            initgvData();
            lblPath.Text = f.getModuleidAdd(Request["moduid"]);
        }
    }

    private void initgvData() 
    {

        string sql = "select SalesCode,comodBarcode,brand,comodname,modul,unit,number,Convert(char(10),DeliveryDate,120) as date,prices, gifts,guestName,GuestMan,phone,address,TotalMoney,IsOut,Isreturn from CS_Sales  a join CS_Guest b on a.guestid=b.GuestId where IsOut=0 and isDel=0 order by SalesId desc";
  
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }
    protected void btnSelec_Click(object sender, EventArgs e)
    {
        string sql = "select SalesCode,comodBarcode,brand,comodname,modul,unit,Convert(char(10),DeliveryDate,120) as date,number,prices, gifts,guestName,GuestMan,phone,address,TotalMoney,IsOut,Isreturn from CS_Sales  a join CS_Guest b on a.guestid=b.GuestId where 1=1 and isDel=0";
        if (!string.IsNullOrEmpty(yjTm1.Text) && !string.IsNullOrEmpty(yjTm2.Text.Trim()))
        {
            sql += " and DeliveryDate>='" + yjTm1.Text.Trim() + " 00:00:00' and DeliveryDate<='" + yjTm2.Text.Trim() + " 23:59:59'";
        }
        if (!string.IsNullOrEmpty(txtBarCode.Text))
        {
            sql += " and b.GuestMan like'%" + txtBarCode.Text + "%'";
        }
        if (!string.IsNullOrEmpty(txtmodul.Text))
        {
            sql += " and modul like'%" + txtmodul.Text + "%'";
        }
        sql += " and IsOut="+ddlisOut.SelectedValue;
        PageNavigator1.Total = 1;
        PageNavigator1.Query = sql;
    }
    protected void Label5_Click(object sender, EventArgs e)
    {
        string SalesCode = (sender as LinkButton).CommandArgument.ToString();
        Response.Redirect("LibraryOut.aspx?SalesCode=" + SalesCode + "&action=zj&moduid=" + Request["moduid"] + "");

    }
    protected void Label6_Click(object sender, EventArgs e)
    {
        string SalesCode = (sender as LinkButton).CommandArgument.ToString();
        ClientScript.RegisterStartupScript(typeof(Page), "aa", "PrintData('" + SalesCode + "')", true);

    }
    protected void GvDataBand_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string AuditState = DataBinder.Eval(e.Row.DataItem, "IsOut").ToString();
            string AuditState1 = DataBinder.Eval(e.Row.DataItem, "Isreturn").ToString();
            string todate = DataBinder.Eval(e.Row.DataItem, "date").ToString();
            if (AuditState == "1")
            {
                if (AuditState1 == "1")
                {
                    (e.Row.Cells[10].FindControl("Label4") as Label).Text = "已退货";
                    (e.Row.Cells[10].FindControl("Label4") as Label).ForeColor = System.Drawing.Color.FromName("#ff0000");
                }
                else
                {
                    (e.Row.Cells[10].FindControl("Label4") as Label).Text = "";
                }
                (e.Row.Cells[10].FindControl("Label3") as Label).Text = "已出";
                (e.Row.Cells[11].FindControl("Label5") as LinkButton).Enabled =false;//出货不可用
                (e.Row.Cells[12].FindControl("ckFlag") as CheckBox).Enabled = false;//删除不可用
                (e.Row.Cells[10].FindControl("Label3") as Label).ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            if (AuditState == "0")
            {
                (e.Row.Cells[10].FindControl("Label3") as Label).Text = "未出";
                (e.Row.Cells[11].FindControl("Label6") as LinkButton).Enabled = false;//打印不可用                
                if (AuditState1 == "0")
                {
                    (e.Row.Cells[10].FindControl("Label4") as Label).Text = "";
                }
                (e.Row.Cells[10].FindControl("Label3") as Label).ForeColor = System.Drawing.Color.FromName("#ff0000");
            }
            if (todate == DateTime.Now.ToString())
            {
                e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#FF0000");
            }

        }

    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        int n = 0;
        if (this.GvDataBand.Rows.Count > 0)
        {
            for (int i = 0; i < GvDataBand.Rows.Count; i++)
            {
                CheckBox ck = GvDataBand.Rows[i].Cells[13].FindControl("ckFlag") as CheckBox;
                if (ck.Checked)
                {
                    string id = (this.GvDataBand.Rows[i].Cells[0].FindControl("Label1") as Label).Text;
                    zs.Open("delete CS_Sales  where SalesCode='" + id + "'");
                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('删除成功');", true);
                }
                else
                {
                    n++;
                }
                if (n == this.GvDataBand.Rows.Count)
                {

                    ClientScript.RegisterStartupScript(typeof(Page), "aa", "alert('至少应选择一项')", true);
                }
            }
        }
        zs.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string all = "tj";
        if (yjTm1.Text == "" && yjTm2.Text == "" && txtBarCode.Text == "" && txtmodul.Text == "" && ddlisOut.SelectedValue=="0")
        {
            all = "all";
        }
        ClientScript.RegisterStartupScript(typeof(Page), "aa", "AllPrintData('" + all + "','" + yjTm1.Text + "','" + yjTm2.Text + "','" + txtBarCode.Text + "','" + txtmodul.Text + "','" + ddlisOut.SelectedValue + "')", true);
    }
    protected void Excel_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=OutManage" + DateTime.Now.Date.ToString("yyyyMMdd") + ".xls");
        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码！！！
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        //  turn off paging 

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        this.GvDataBand.RenderControl(htw);
        string strHtml = sw.ToString().Trim();
        Response.Output.Write(strHtml);
        Response.Flush();
        Response.End();
        // turn the paging on again 

    }
    //添加一个空方法,防止   GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内错误
    public override void VerifyRenderingInServerForm(Control control)
    {
        // Confirms that an HtmlForm. control is rendered for
    }
}
